package com.xiaoshuidi.cloud.module.member.service.message;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.member.api.message.dto.MemberMessageDTO;
import com.xiaoshuidi.cloud.module.member.controller.app.message.vo.MessageListVO;
import com.xiaoshuidi.cloud.module.member.controller.app.message.vo.MessagePageVO;
import com.xiaoshuidi.cloud.module.member.enums.MessageTypeEnum;
import com.xiaoshuidi.cloud.module.member.mapper.message.MemberMessageMapper;
import com.xiaoshuidi.cloud.module.member.pojo.comment.MemberComment;
import com.xiaoshuidi.cloud.module.member.pojo.message.MemberMessage;
import com.xiaoshuidi.cloud.module.member.repository.MemberCommentRepository;
import com.xiaoshuidi.cloud.module.member.repository.MemberMessageRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.MESSAGE_NOT_EXIST;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.NO_USER;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberMessageServiceImpl extends ServiceImpl<MemberMessageMapper, MemberMessage> implements MemberMessageService {
    private final MemberMessageRepository memberMessageRepository;
    private final MemberCommentRepository memberCommentRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean create(MemberMessageDTO message) {
        MemberMessage memberMessage = memberMessageRepository.create(message);
        //如果消息类型是评价，需新增一条未评价记录
        if (message.getType().intValue() == MessageTypeEnum.EVALUATED.getCode().intValue()) {
            MemberComment memberComment = new MemberComment();
            memberComment.setCommentType(message.getCommentType());
            memberComment.setApartmentName(message.getApartmentName());
            memberComment.setRoomId(message.getRoomId());
            memberComment.setCreator(message.getUserId().toString());
            memberComment.setUpdater(message.getUserId().toString());
            memberComment.setSignDay(Date.valueOf(message.getSignDay()));
            memberComment.setSteward(message.getSteward());
            memberComment.setStewardId(message.getStewardId());
            memberComment.setMessageId(memberMessage.getId());
            memberCommentRepository.createUnComment(memberComment);
        }
        return true;
    }

    @Override
    public PageResult<MessageListVO> getAppPage(MessagePageVO pageVO) {
        return memberMessageRepository.getAppPage(pageVO);
    }

    @Override
    public Boolean updateStatus(Long id) {

        MemberMessage message = memberMessageRepository.getById(id);
        if (message == null) {
            throw exception(MESSAGE_NOT_EXIST);
        }
        message.setStatus(2);
        memberMessageRepository.updateById(message);
        return true;
    }

    @Override
    public void updateUserId(Long oldUserId, Long userId) {
        this.baseMapper.updateUserId(oldUserId,userId);
    }
}
